/*
 *   Swing Explorer. Tool for developers exploring Java/Swing-based application internals. 
 * 	 Copyright (C) 2012, Maxim Zakharenkov
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *   
 */
package org.swingexplorer.additiontrace;

import java.awt.Component;
import java.awt.Rectangle;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.StringReader;

import javax.swing.text.AbstractDocument;
import javax.swing.text.EditorKit;

import org.swingexplorer.GuiUtils;
import org.swingexplorer.MdlSwingExplorer;
import org.swingexplorer.idesupport.IDESupport;
import org.swingexplorer.instrument.Agent;

/**
 * 
 * @author Maxim Zakharenkov
 */
public class PNLAdditionTrace extends javax.swing.JPanel {
    
    /** Creates new form PNLAdditionTrace */
    public PNLAdditionTrace() {
        initComponents();
        
        txtTrace.setEditorKit(new NoWrapEditorKit());
        initActions();
    }

	/** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scpTrace = new javax.swing.JScrollPane();
        txtTrace = new javax.swing.JEditorPane();

        txtTrace.setContentType("text/html");
        txtTrace.setEditable(false);
        scpTrace.setViewportView(txtTrace);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(scpTrace, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(scpTrace, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    javax.swing.JScrollPane scpTrace;
    javax.swing.JEditorPane txtTrace;
    // End of variables declaration//GEN-END:variables

    private void initActions() {
    	actOpenSourceCode = new ActOpenSourceCode(this);
        txtTrace.addHyperlinkListener(actOpenSourceCode);
	}

    
    MdlSwingExplorer model;
    ModelListener modelListener = new ModelListener();
    ActOpenSourceCode actOpenSourceCode;
    
	public void setModel(MdlSwingExplorer _model) {
		model = _model;
		model.addPropertyChangeListener(modelListener);		
	}
	
	private void setStackTrace(StackTraceElement[] stackTrace) {
		if(stackTrace == null) {
			txtTrace.setText("<html><p>Stack trace is not available for the selection</p></html>");
			return;
		}
		
		StringBuilder buf = new StringBuilder("<html>");
		for(StackTraceElement elem : stackTrace) {
			buf.append("<p>");
			buf.append(GuiUtils.formatElementToHTML(elem));
			buf.append("</p>");
		}
		buf.append("<html>");
		
		// this section is done to be able scroll
		// to the beginning after text is set to JEditorPane
		EditorKit edKit = txtTrace.getEditorKit();
		AbstractDocument doc = (AbstractDocument) edKit.createDefaultDocument();//(HTMLDocument) txaAbout.getDocument(); //new HTMLDocument();
        doc.setAsynchronousLoadPriority(-1);
        try {
            edKit.read(new StringReader(buf.toString()), doc, 0);
        } catch (Exception e) {
            e.printStackTrace();
        } 
        txtTrace.setDocument(doc);
        
        // here is scrolling finally
        txtTrace.scrollRectToVisible(new Rectangle(0, 0, 1, 1));
	}
	
	
	class ModelListener implements PropertyChangeListener {
		public void propertyChange(PropertyChangeEvent evt) {
			if("selectedComponents".equals(evt.getPropertyName())) {
				Component[] selected = model.getSelectedComponents();
				if(selected != null && selected.length > 0) {
					StackTraceElement[] stackTrace = Agent.getAddImplStackTrace(selected[0]);
					setStackTrace(stackTrace);
				}
			}
		}
	}

	public void setIDESupport(IDESupport _ideSupport) {
		actOpenSourceCode.ideSupport = _ideSupport;
	}
}

